/**
 *	Contains the win checker object and code
 *
 *	each function in here returns a number out of the following 4 values:
 *		-1	:	game ended in a draw
 *		0	:	game ongoing
 *		1	:	player 1 win
 *		2	:	player 2 win
**/

"use strict"

if (typeof GameLogic === "undefined") {
	var GameLogic = { }
}

GameLogic.winChecker = {
	// contains all the rules
	_rules: { },
	
	// rules modules (seperate files) can add rules using this function
	addRules: function(rules) {
		for (var r in rules) {
			if (typeof this._rules[r] !== "undefined") {
				throw "winChecker addrules: rule '" + r + "' already defined"
			}
			this._rules[r] = rules[r]
		}
	},
	
	// main entry into the checker
	check: function(game) {
		//this.game = game
		
		// make sure rules are appraised in order
		var ruleorder = game.winConditions
		
		// loop through all rules and check for a winner
		var result
		for (var i=0, ii=ruleorder.length; i<ii; i++) {
			result = this._rules[ ruleorder[i] ](game)		// run the rule
			if (result !== 0)  return result				// return a winner if found
		}
		return 0											// no winner, return 0 (game in progress)
	},
		
}
